<article id="wikiArticle">
<div></div>
<h2 id="错误提示">错误提示</h2>
<pre><code class="language-javascript">TypeError: can't access dead object
</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError（类型错误） 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
<h2 id="哪里出错了？">哪里出错了？</h2>
<p>为了提高内存使用效率以及防止内存泄露，Firefox 浏览器不允许插件在 DOM 所在的父页面被销毁后对 DOM 对象保持强引用。死对象指的是在 DOM 被销毁后依然持有对 DOM 元素的强引用(处于活跃状态)。为了避免这样的问题，对处于外部文档中的 DOM 节点的引用应该被存储于一个专属于那个文档的对象当中，并且在文档卸载的时候将其清理，或者使用<a href="/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.getWeakReference">弱引用</a>方式进行存储。</p>
<h2 id="Checking_if_an_object_is_dead">Checking if an object is dead</h2>
<p><a href="/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils">Components.utils</a> offers a <code>isDeadWrapper()</code> method, which privileged code might use.</p>
<pre><code  class="language-javascript">if (Components.utils.isDeadWrapper(window)) {
  // dead
}</code></pre>
<p>Unprivileged code has no access to Component.utils and might just be able catch the exception.</p>
<pre><code  class="language-javascript">try {
  String(window);
}
catch (e) {
  console.log("window is likely dead");
}</code></pre>
<h2 id="相关内容">相关内容</h2>
<ul>
<li><a class="external" href="https://blog.mozilla.org/addons/2012/09/12/what-does-cant-access-dead-object-mean/" rel="noopener">What does “can’t access dead object” mean?</a></li>
<li><a href="/en-US/docs/Extensions/Common_causes_of_memory_leaks_in_extensions">Common causes of memory leaks in extensions</a></li>
<li><a href="/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils">Components.utils</a></li>
<li><a href="/en-US/docs/Mozilla/Zombie_compartments">Zombie Compartments</a></li>
</ul>
</article>